package com.common.android.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListView;

public class AutoHideKeyboardListView extends ListView {

	public AutoHideKeyboardListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			Context ctx = (Context) getContext();
			if (ctx != null) {
				if (ctx instanceof Activity) {
					Activity activity = (Activity) ctx;
					View focusView = activity.getCurrentFocus();
					if (focusView != null) {
						InputMethodManager imm = (InputMethodManager) getContext()
								.getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(focusView.getWindowToken(),
								0);
					}
				}
			}
		}

		return super.onInterceptTouchEvent(ev);
	}

}
